/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseDbSortLogic.cs
*        创建系统时间:       2012/11/2 19:06:59
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using ECP.DataAccess.Utilities;
    using ECP.Utilities;

    public class BaseDbSortLogic
    {
        public const string CommandSetBottom = "SetBottom";
        public const string CommandSetDown = "SetDown";
        public const string CommandSetTop = "SetTop";
        public const string CommandSetUp = "SetUp";
        public const string CommandSwap = "Swap";

        public static string GetDownID(IDbHelper dbHelper, string tableName, string id)
        {
            return GetDownID(dbHelper, tableName, string.Empty, id);
        }

        public static string GetDownID(IDbHelper dbHelper, string tableName, string categoryID, string id)
        {
            string str = string.Empty;
            string sqlQuery = string.Empty;
            if (categoryID.Length > 0)
            {
                str = BaseBusinessLogic.FieldCategoryID + " = '" + categoryID + "' And ";
            }
            sqlQuery = "SELECT TOP 1 " + BaseBusinessLogic.FieldID + " FROM " + tableName + " WHERE ( " + str + BaseBusinessLogic.FieldSortCode + " < (SELECT TOP 1 " + BaseBusinessLogic.FieldSortCode + " FROM " + tableName + " WHERE ( " + str + BaseBusinessLogic.FieldID + " = '" + id + "' )  ORDER BY " + BaseBusinessLogic.FieldSortCode + " )) ORDER BY " + BaseBusinessLogic.FieldSortCode + " DESC ";
            return DbLogic.ExecuteScalar(dbHelper, sqlQuery);
        }

        public static string GetUpID(IDbHelper dbHelper, string tableName, string id)
        {
            return GetUpID(dbHelper, tableName, string.Empty, id);
        }

        public static string GetUpID(IDbHelper dbHelper, string tableName, string categoryID, string id)
        {
            string str = string.Empty;
            string sqlQuery = string.Empty;
            if (categoryID.Length > 0)
            {
                str = BaseBusinessLogic.FieldCategoryID + " = '" + categoryID + "' AND ";
            }
            sqlQuery = " SELECT TOP 1 " + BaseBusinessLogic.FieldID + " FROM " + tableName + " WHERE ( " + str + BaseBusinessLogic.FieldSortCode + " > (SELECT TOP 1 " + BaseBusinessLogic.FieldSortCode + " FROM " + tableName + " WHERE ( " + str + BaseBusinessLogic.FieldID + " = '" + id + "' )  ORDER BY " + BaseBusinessLogic.FieldSortCode + " ))  ORDER BY " + BaseBusinessLogic.FieldSortCode + " ASC ";
            return DbLogic.ExecuteScalar(dbHelper, sqlQuery);
        }

        public static int SetBottom(IDbHelper dbHelper, string tableName, string id)
        {
            string sequence = BaseSequenceDao.Instance.GetSequence(dbHelper, tableName);
            return DbLogic.SetProperty(dbHelper, tableName, BaseBusinessLogic.FieldID, id, BaseBusinessLogic.FieldSortCode, sequence);
        }

        public static int SetBottom(IDbHelper dbHelper, string tableName, string id, string sequenceName)
        {
            if (string.IsNullOrEmpty(sequenceName))
            {
                sequenceName = tableName;
            }
            string sequence = BaseSequenceDao.Instance.GetSequence(dbHelper, sequenceName);
            return DbLogic.SetProperty(dbHelper, tableName, BaseBusinessLogic.FieldID, id, BaseBusinessLogic.FieldSortCode, sequence);
        }

        public static int SetDown(IDbHelper dbHelper, string tableName, string id)
        {
            return SetDown(dbHelper, tableName, string.Empty, id);
        }

        public static int SetDown(IDbHelper dbHelper, string tableName, string categoryID, string id)
        {
            string str = string.Empty;
            string targetValue = string.Empty;
            string str3 = string.Empty;
            int num = 0;
            str = GetDownID(dbHelper, tableName, categoryID, id);
            if (str.Length == 0)
            {
                return num;
            }
            targetValue = DbLogic.GetProperty(dbHelper, tableName, BaseBusinessLogic.FieldID, id, BaseBusinessLogic.FieldSortCode);
            str3 = DbLogic.GetProperty(dbHelper, tableName, BaseBusinessLogic.FieldID, str, BaseBusinessLogic.FieldSortCode);
            DbLogic.SetProperty(dbHelper, tableName, BaseBusinessLogic.FieldID, str, BaseBusinessLogic.FieldSortCode, targetValue);
            return DbLogic.SetProperty(dbHelper, tableName, BaseBusinessLogic.FieldID, id, BaseBusinessLogic.FieldSortCode, str3);
        }

        public static int SetTop(IDbHelper dbHelper, string tableName, string id)
        {
            string degression = BaseSequenceDao.Instance.GetDegression(dbHelper, tableName);
            return DbLogic.SetProperty(dbHelper, tableName, BaseBusinessLogic.FieldID, id, BaseBusinessLogic.FieldSortCode, degression);
        }

        public static int SetTop(IDbHelper dbHelper, string tableName, string id, string sequenceName)
        {
            if (string.IsNullOrEmpty(sequenceName))
            {
                sequenceName = tableName;
            }
            string degression = BaseSequenceDao.Instance.GetDegression(dbHelper, sequenceName);
            return DbLogic.SetProperty(dbHelper, tableName, BaseBusinessLogic.FieldID, id, BaseBusinessLogic.FieldSortCode, degression);
        }

        public static int SetUp(IDbHelper dbHelper, string tableName, string id)
        {
            return SetUp(dbHelper, tableName, string.Empty, id);
        }

        public static int SetUp(IDbHelper dbHelper, string tableName, string categoryID, string id)
        {
            string str = GetUpID(dbHelper, tableName, categoryID, id);
            string targetValue = string.Empty;
            string str3 = string.Empty;
            int num = 0;
            if (str.Length == 0)
            {
                return num;
            }
            targetValue = DbLogic.GetProperty(dbHelper, tableName, BaseBusinessLogic.FieldID, id, BaseBusinessLogic.FieldSortCode);
            str3 = DbLogic.GetProperty(dbHelper, tableName, BaseBusinessLogic.FieldID, str, BaseBusinessLogic.FieldSortCode);
            return (DbLogic.SetProperty(dbHelper, tableName, BaseBusinessLogic.FieldID, str, BaseBusinessLogic.FieldSortCode, targetValue) + DbLogic.SetProperty(dbHelper, tableName, BaseBusinessLogic.FieldID, id, BaseBusinessLogic.FieldSortCode, str3));
        }

        public static int Swap(IDbHelper dbHelper, string tableName, string id, string targetID)
        {
            int num = 0;
            string targetValue = DbLogic.GetProperty(dbHelper, tableName, BaseBusinessLogic.FieldID, id, BaseBusinessLogic.FieldSortCode);
            string str2 = DbLogic.GetProperty(dbHelper, tableName, BaseBusinessLogic.FieldID, targetID, BaseBusinessLogic.FieldSortCode);
            num += DbLogic.SetProperty(dbHelper, tableName, BaseBusinessLogic.FieldID, id, BaseBusinessLogic.FieldSortCode, str2);
            return (num + DbLogic.SetProperty(dbHelper, tableName, BaseBusinessLogic.FieldID, targetID, BaseBusinessLogic.FieldSortCode, targetValue));
        }
    }
}